package com.example.gokuplayalong.bases.traces;

import com.example.gokuplayalong.bases.beans.AppParams;
import com.example.gokuplayalong.bases.exception.ExceptionUtils;
import com.example.gokuplayalong.bases.tools.Tools;

import java.io.File;
import java.io.FileWriter;

public class UserTraces {
    static File fileTrace = null;

    public static void init() {

        try {
            fileTrace = new File(AppParams.getInstance().mediaDir, "userTrace.txt");
            if (!fileTrace.exists()) {
                fileTrace.createNewFile();
            }
        } catch (Throwable e) {
            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);

        }
    }

    public static void writeTraces(String traces) {
        try {
            if (fileTrace == null) {
                init();
            }
            FileWriter fileWriter;
            if (fileTrace.length() > 10 * 1024 * 1024) {
                fileWriter = new FileWriter(fileTrace.getAbsoluteFile(), false);
            }
            else {
                fileWriter = new FileWriter(fileTrace.getAbsoluteFile(), true);
            }

            fileWriter.write("\n" + Tools.getSDF().format(System.currentTimeMillis()) + "：");
            fileWriter.write(traces);
            fileWriter.close();


        } catch (Throwable e) {
            ExceptionUtils.getException(ExceptionUtils.getTraceInfo(), e);
        }

    }
}
